﻿using Framework.Poco;
using Shell.DomainDot;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Framework.Domain
{
    /// <summary>
    /// 经销商的上下文
    /// </summary>
    public class AgencyContext : DomainDotBase<Agency>
    {
        /// <summary>
        /// 与经销商相关的联系人
        /// </summary>
        public WithMany<Contact> Contacts { private get; set; }

        /// <summary>
        /// 属于经销商的市区
        /// </summary>
        public WithOne<District> DistrictBelonged { private get; set; }

        /// <summary>
        /// 属于经销商的经销商分组
        /// </summary>
        public WithOne<AgencyGroup> AgencyGroupBelonged { private get; set; }

        /// <summary>
        /// 获取属于经销商的经销商分组
        /// </summary>
        public AgencyGroup GetAgencyGroup()
        {
            return this.AgencyGroupBelonged.Instance;
        }

        /// <summary>
        /// 获取属于经销商的城区
        /// </summary>
        public District GetDistrict()
        {
            return this.DistrictBelonged.Instance;
        }

        /// <summary>
        /// 获取所有属于经销商的联系人
        /// </summary>
        public IEnumerable<Contact> GetAllContact()
        {
            return this.Contacts;
        }

        /// <summary>
        /// 创建属于经销商的联系人
        /// </summary>
        public void CreateContact(Contact contact)
        {
            contact.Id = Guid.NewGuid();
            contact.IsDefault = true;
            var entity = this.Contacts.Create();
            PocoUtils.AssignProperty<Contact>(contact, entity, typeof(Guid));
            this.Contacts.Submit();
        }

        /// <summary>
        /// 添加属于经销商的联系人
        /// </summary>
        public void AddContact(Contact contact)
        {
            var entity = this.Contacts.Create();
            PocoUtils.AssignProperty<Contact>(contact, entity, typeof(Guid));
            this.Contacts.Submit();
        }

        /// <summary>
        /// 修改属于经销商的联系人
        /// </summary>
        public void EditContact(Contact contact)
        {
            var original = this.GetAllContact().SingleOrDefault(o => o.Id == contact.Id);
            if (original != null)
            {
                original.Email = contact.Email;
                original.Fax = contact.Fax;
                original.Mobile = contact.Mobile;
                original.MSN = contact.Mobile;
                original.Name = contact.Name;
                original.Phone = contact.Phone;
                original.QQ = contact.QQ;
                this.Contacts.Submit();
            }
        }

        /// <summary>
        /// 删除属于经销商的联系人
        /// </summary>
        public void DeleteContact(Guid identity)
        {
            var original = this.GetAllContact().SingleOrDefault(o => o.Id == identity);
            if (original != null)
            {
                this.Contacts.Delete(identity);
                this.Contacts.Submit();
            }
        }

        /// <summary>
        /// 删除属于经销商的所有联系人
        /// </summary>
        public void DeleteContacts()
        {
            //foreach (var item in this.Contacts)
            //{
            //    this.DeleteContact(item.Id);
            //}
            foreach (var item in this.Contacts.ToList())
            {
                this.Contacts.Delete(item.Id);
            }
            this.Contacts.Submit();
        }

        /// <summary>
        /// 绑定属于经销商的地区
        /// </summary>
        public void BindDistrict(Guid identity)
        {
            this.DistrictBelonged.Bind(identity);
            this.DistrictBelonged.Submit();
        }

        /// <summary>
        /// 释放属于经销商的城区
        /// </summary>
        public void ReleaseDistrict()
        {
            this.DistrictBelonged.Release();
            this.DistrictBelonged.Submit();
        }

        /// <summary>
        /// 绑定属于经销商的分组
        /// </summary>
        public void BindAgencyGroup(Guid identity)
        {
            this.AgencyGroupBelonged.Bind(identity);
            this.AgencyGroupBelonged.Submit();
        }

        /// <summary>
        /// 释放属于经销商的分组
        /// </summary>
        public void ReleaseAgencyGroup()
        {
            this.AgencyGroupBelonged.Release();
            this.AgencyGroupBelonged.Submit();
        }
    }
}
